package weaver.interfaces.linearin.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.linearin.duss.utils.KingdeeUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-12-05
 * @Description: 支付申请（先积）流程归档后调用生成凭证接口
 * @ModifyLog:
 **/
public class WeaZfsqlcBBAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaZfsqlcBBAction>>>>>requestid=" + requestid);
        try {
            JSONObject dataJson = new JSONObject();
            String message = "调用金蝶系统接口返回:";
            String bookid = "003";
            String organid = "1003";
            RecordSet rs = new RecordSet();
            rs.executeQuery("select id,lcbh,fkrq from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String lcbh = Util.null2String(rs.getString("lcbh"));
            String fkrq = Util.null2String(rs.getString("fkrq"));
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaZfsqlcBBAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("bookid", bookid);
            dataMap.put("cz", src);
            int count = 0;
            //其他付款单
            rs.executeQuery("select fkrq,skdw,bz1,zfzh,sum(sjfkjeyb) sjfkjeyb from " + tablename + "_dt1 " +
                    "where mainid=? and scdjlx=0 group by fkrq,skdw,bz1,zfzh", mainid);
            while (rs.next()) {
                JSONObject paramsJson = new JSONObject();
                String fkrq1 = Util.null2String(rs.getString("fkrq"));
                String skdw = Util.null2String(rs.getString("skdw"));
                String bz1 = Util.null2String(rs.getString("bz1"));
                String zfzh = Util.null2String(rs.getString("zfzh"));
                String sjfkjeyb = Util.null2String(rs.getString("sjfkjeyb"));
                paramsJson.put("rq", fkrq1);
                paramsJson.put("jdrybm", KingdeeUtil.getGysBm(skdw));
                paramsJson.put("bz", "0".equals(bz1) ? "PRE001" : "USD");
                paramsJson.put("je", sjfkjeyb);
                paramsJson.put("zfzh", zfzh);
                paramsJson.put("zz", organid);
                JSONObject resultJson = KingdeeUtil.otherBusinessPay(paramsJson, dataMap);
                writeLog(">>>>>WeaZfsqlcBBAction>>>>>resultJson=" + resultJson.toJSONString());
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String dataStr = Util.null2String(resultJson.getString("data"));
                    JSONObject retrunDataJson = JSONObject.parseObject(dataStr);
                    JSONObject ResultJson = JSONObject.parseObject(retrunDataJson.getString("Result"));
                    JSONObject ResponseStatusJson = JSONObject.parseObject(ResultJson.getString("ResponseStatus"));
                    String IsSuccess = Util.null2String(ResponseStatusJson.getString("IsSuccess"));
                    if ("false".equals(IsSuccess)) {
                        message += Util.null2String(ResponseStatusJson.getString("Errors"));
                        count++;
                    }
                }
            }
            //采购付款单
            rs.executeQuery("select fkrq,skdw,bz1,zfzh,sum(sjfkjeyb) sjfkjeyb from " + tablename + "_dt1 " +
                    "where mainid=? and scdjlx=1 group by fkrq,skdw,bz1,zfzh", mainid);
            while (rs.next()) {
                JSONObject paramsJson = new JSONObject();
                String fkrq1 = Util.null2String(rs.getString("fkrq"));
                String skdw = Util.null2String(rs.getString("skdw"));
                String bz1 = Util.null2String(rs.getString("bz1"));
                String zfzh = Util.null2String(rs.getString("zfzh"));
                String sjfkjeyb = Util.null2String(rs.getString("sjfkjeyb"));
                paramsJson.put("rq", fkrq1);
                paramsJson.put("jdrybm", KingdeeUtil.getGysBm(skdw));
                paramsJson.put("bz", "0".equals(bz1) ? "PRE001" : "USD");
                paramsJson.put("je", sjfkjeyb);
                paramsJson.put("zfzh", zfzh);
                paramsJson.put("zz", organid);
                JSONObject resultJson = KingdeeUtil.procurementBusinessPay(paramsJson, dataMap);
                writeLog(">>>>>WeaZfsqlcBBAction>>>>>resultJson=" + resultJson.toJSONString());
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String dataStr = Util.null2String(resultJson.getString("data"));
                    JSONObject retrunDataJson = JSONObject.parseObject(dataStr);
                    JSONObject ResultJson = JSONObject.parseObject(retrunDataJson.getString("Result"));
                    JSONObject ResponseStatusJson = JSONObject.parseObject(ResultJson.getString("ResponseStatus"));
                    String IsSuccess = Util.null2String(ResponseStatusJson.getString("IsSuccess"));
                    if ("false".equals(IsSuccess)) {
                        message += Util.null2String(ResponseStatusJson.getString("Errors"));
                        count++;
                    }
                }
            }
            //凭证
            //单据体：FEntity
            JSONArray FEntity = new JSONArray();
            /*单据体第一行*/
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=? and scdjlx=2", mainid);
            while (rs.next()) {
                JSONObject FEntityJSON = new JSONObject();
                String skdw = Util.null2String(rs.getString("skdw"));
                String bz1 = Util.null2String(rs.getString("bz1"));
                String hl = Util.null2String(rs.getString("hl"));
                String sjfkjeyb = Util.null2String(rs.getString("sjfkjeyb"));
                String sjfkrmb = Util.null2String(rs.getString("sjfkrmb"));
                //摘要
                String fexplanation = lcbh + "-支付-" + KingdeeUtil.getGysmc(skdw);
                FEntityJSON.put("FEXPLANATION", fexplanation);
                //科目编码
                JSONObject FACCOUNTID = new JSONObject();
                FACCOUNTID.put("FNumber", "2241.02");
                FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                //核算维度
                JSONObject FDetailID = new JSONObject();
                //收款单位
                JSONObject FDETAILID__FFLEX4 = new JSONObject();
                FDETAILID__FFLEX4.put("FNumber", KingdeeUtil.getGysBm(skdw));
                FDetailID.put("FDETAILID__FFLEX4", FDETAILID__FFLEX4);
                FEntityJSON.put("FDetailID", FDetailID);
                //币别
                bz1 ="0".equals(bz1) ? "PRE001" : "USD";
                JSONObject FCURRENCYID = new JSONObject();
                FCURRENCYID.put("FNumber", bz1);
                FEntityJSON.put("FCURRENCYID", FCURRENCYID);
                //汇率类型
                JSONObject FEXCHANGERATETYPE = new JSONObject();
                FEXCHANGERATETYPE.put("FNumber", "HLTX01_SYS");
                FEntityJSON.put("FEXCHANGERATETYPE", FEXCHANGERATETYPE);
                //汇率
                FEntityJSON.put("FEXCHANGERATE", hl);
                //原币金额
                FEntityJSON.put("FAMOUNTFOR", sjfkjeyb);
                //借方金额
                FEntityJSON.put("FDEBIT", sjfkrmb);
                FEntity.add(FEntityJSON);
            }
            /*单据体第二行*/
            rs.executeQuery("select skdw,bz1,hl,sum(sjfkjeyb) sjfkjeyb,sum(sjfkrmb) sjfkrmb,zfzhd from " + tablename + "_dt1 " +
                    "where mainid=? and scdjlx=2 group by skdw,bz1,hl,zfzhd", mainid);
            while (rs.next()) {
                JSONObject FEntityJSON = new JSONObject();
                String skdw = Util.null2String(rs.getString("skdw"));
                String bz1 = Util.null2String(rs.getString("bz1"));
                String hl = Util.null2String(rs.getString("hl"));
                String sjfkjeyb = Util.null2String(rs.getString("sjfkjeyb"));
                String sjfkrmb = Util.null2String(rs.getString("sjfkrmb"));
                String zfzhd = Util.null2String(rs.getString("zfzhd"));
                //摘要
                String fexplanation = lcbh + "-支付-" + KingdeeUtil.getGysmc(skdw);
                FEntityJSON.put("FEXPLANATION", fexplanation);
                //科目编码
                JSONObject FACCOUNTID = new JSONObject();
                FACCOUNTID.put("FNumber", "1002.01");
                FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                //核算维度
                JSONObject FDetailID = new JSONObject();
                //收款单位
                //银行账号
                JSONObject FDETAILID__FF100006 = new JSONObject();
                FDETAILID__FF100006.put("FNumber", zfzhd);
                FDetailID.put("FDETAILID__FF100006", FDETAILID__FF100006);
                FEntityJSON.put("FDetailID", FDetailID);
                //币别
                bz1 ="0".equals(bz1) ? "PRE001" : "USD";
                JSONObject FCURRENCYID = new JSONObject();
                FCURRENCYID.put("FNumber", bz1);
                FEntityJSON.put("FCURRENCYID", FCURRENCYID);
                //汇率类型
                JSONObject FEXCHANGERATETYPE = new JSONObject();
                FEXCHANGERATETYPE.put("FNumber", "HLTX01_SYS");
                FEntityJSON.put("FEXCHANGERATETYPE", FEXCHANGERATETYPE);
                //汇率
                FEntityJSON.put("FEXCHANGERATE", hl);
                //原币金额
                FEntityJSON.put("FAMOUNTFOR", sjfkjeyb);
                //贷方金额
                FEntityJSON.put("FCREDIT", sjfkrmb);
                FEntity.add(FEntityJSON);
            }
            dataJson.put("zfrq", fkrq);
            dataJson.put("FEntity", FEntity);
            if(FEntity.size()>0){
                writeLog(">>>>>WeaZfsqlcBBAction>>>>>dataJson=" + dataJson.toJSONString());
                JSONObject resultJson = KingdeeUtil.passCertificate(dataJson, dataMap);
                writeLog(">>>>>WeaZfsqlcBBAction>>>>>resultJson=" + resultJson.toJSONString());
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String dataStr = Util.null2String(resultJson.getString("data"));
                    JSONObject retrunDataJson = JSONObject.parseObject(dataStr);
                    JSONObject ResultJson = JSONObject.parseObject(retrunDataJson.getString("Result"));
                    JSONObject ResponseStatusJson = JSONObject.parseObject(ResultJson.getString("ResponseStatus"));
                    String IsSuccess = Util.null2String(ResponseStatusJson.getString("IsSuccess"));
                    if ("false".equals(IsSuccess)) {
                        message += Util.null2String(ResponseStatusJson.getString("Errors"));
                        count++;
                    }
                }
                if (count > 0) {
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}